package com.vhr.service.impl;

import com.vhr.VO.EmpVO;
import com.vhr.VO.LeaderVO;
import com.vhr.VO.TrainVO;
import com.vhr.entities.*;
import com.vhr.form.TrainForm;
import com.vhr.mapper.EmployeeMapper;
import com.vhr.mapper.TrainMapper;
import com.vhr.mapper.Train_empMapper;
import com.vhr.service.ITrainService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class ITrainServiceImpl implements ITrainService{
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private TrainMapper trainMapper;

    @Autowired
    private Train_empMapper train_empMapper;

    /*根据员工查询所属培训班的*/
    @Override
    public List<TrainVO> selectByEmployee(Employee emp){
        //添加员工id为查询条件,查询出该员工的培训班-员工关系表信息
        Train_empExample train_empExample = new Train_empExample();
        train_empExample.createCriteria().andEmpIdEqualTo(emp.getEmpId());
        List<Train_emp> train_emp = train_empMapper.selectByExample(train_empExample);

        List<Train> trainList = new ArrayList<Train>();

        //根据培训班-员工关系表中的培训班id查出培训班信息
        for(Train_emp list:train_emp){
            Train train = trainMapper.selectByPrimaryKey(list.getTrainId());
            trainList.add(train);
        }

        List<TrainVO> trainVOS = toVO(trainList);

        return trainVOS;
    }

    /*根据培训班主键查询出该培训班员工*/
    @Override
    public List<Employee> selectByPrimaryKey(Train train) {
        Train_empExample train_empExample = new Train_empExample();
        train_empExample.createCriteria().andTrainIdEqualTo(train.getTrainId());
        List<Train_emp> train_empList = train_empMapper.selectByExample(train_empExample);
        List<Employee> employees = new ArrayList<Employee>();
        for(Train_emp list:train_empList){
            Employee employee = new Employee();
            employee = employeeMapper.selectByPrimaryKey(list.getEmpId());
            employees.add(employee);
        }
        return employees;
    }

    /*根据培训班其他条件查询(培训人数、培训开始时间、培训班名称)*/
    @Override
    public List<TrainVO> selectByExample(Train train){
        TrainExample trainExample = new TrainExample();
        TrainExample.Criteria criteria =trainExample.createCriteria();

        if(train.getTrainCount()!=null){            //人数
            criteria.andTrainCountEqualTo(train.getTrainCount());
        }
        if(train.getTrainName()!=null){             //名称(Content是内容)
            criteria.andTrainNameEqualTo(train.getTrainName());
        }

        List<Train> trainList = trainMapper.selectByExample(trainExample);

        List<TrainVO> trainVOS = toVO(trainList);

        return trainVOS;
    }

    /*添加一个培训班*/
    @Transactional
    @Override
    public int insertTrain(TrainForm trainForm) {
        Integer leaderId = employeeMapper.getEmpIdByLeaderName(trainForm.getEmpName());
        Train train = new Train();
        BeanUtils.copyProperties(trainForm,train);
        train.setLeaderId(leaderId);

        int result = trainMapper.insertSelective(train);
        //int result = trainMapper.insertSelective(train);
        //return result;
        return result;
    }

    /*根据培训班主键更新培训班*/
    @Transactional
    @Override
    public int updateTrainByPrimaryKey(Train train) {
        int result = trainMapper.updateByPrimaryKeySelective(train);

        return result;
    }

    /*添加员工到培训班中*/
    @Transactional
    @Override
    public int insertEmployee(List<Integer> empId, Integer trainId) {
        int result =1;
        for(Integer id:empId){
            Train_emp train_emp = new Train_emp();
            train_emp.setEmpId(id);
            train_emp.setTrainId(trainId);
            result = train_empMapper.insertSelective(train_emp);
        }
        return result;
    }

    /*在培训班中删除某个员工*/
    @Transactional
    @Override
    public int deleteEmployee(List<Integer> empId, Integer trainId) {
        int result = 0;
        for(Integer id:empId){
            Train_emp train_emp = train_empMapper.selectByEmpIdAndTrainId(id,trainId);
            // System.out.println("待删除培训班关系表"+train_emp.toString());
            // System.out.println("待删的id："+train_emp.getId());
            result = train_empMapper.deleteByPrimaryKey(train_emp.getId());
            //System.out.println("result："+result);
        }
        return result;
    }

    /*显示所有培训课程*/
    @Override
    public List<TrainVO> selectAll(){
        //List<Department > deptList = departmentMapper.selectByExample(null);
        List<Train> trainList = trainMapper.selectByExample(null);
        List<TrainVO> trainVOList = toVO(trainList);
        return trainVOList;
    }
    /*删除培训课程*/
    @Override
    public int deleteTrain(Integer trainId) {
        int result = trainMapper.deleteByPrimaryKey(trainId);
        return result;
    }
    /*根据培训课程主键查找培训课程*/
    @Override
    public TrainForm selectByPrimary(Integer trainId) {
        Train train = trainMapper.selectByPrimaryKey(trainId);
        Employee employee = employeeMapper.selectByPrimaryKey(train.getLeaderId());
       // System.out.println("负责人名字："+employee.getEmpName());
        TrainForm trainForm = new TrainForm();
        BeanUtils.copyProperties(train,trainForm);
        trainForm.setEmpName(employee.getEmpName());

        //trainForm.setTrain(train);
        //trainForm.setEmpName(employee.getEmpName());
        return trainForm;
    }
    /*根据培训内容或时间查找课程*/
    @Override
    public List<TrainVO> selectByContentOrTime(String trainContent, String trainTime){
        List<Train> list = trainMapper.select(trainContent,trainTime);
        List<TrainVO> trainVOList =toVO(list);
        return trainVOList;
    }
    /*获取10个培训负责人*/
    @Override
    public List<LeaderVO> getLeaders() {
        List<LeaderVO> leaderVOS = employeeMapper.getLeaders();
        return leaderVOS;
    }
    //列举员工带有状态码(0在培训课程，1不在)
    @Override
    public List<EmpVO> listAllEmpByStatus(Integer trainId) {
        List<EmpVO> empVOList = new ArrayList<EmpVO>();
        Train train = new Train();
        train.setTrainId(trainId);
        //在培训课程的员工
        List<Employee> inTrainList = selectByPrimaryKey(train);

        List<Integer> idList = new ArrayList<Integer>();

        if(inTrainList.size()>0){
            for(Employee list1:inTrainList){
                EmpVO empVO = new EmpVO(list1.getEmpId(),list1.getEmpName(),0);
                idList.add(list1.getEmpId());
                empVOList.add(empVO);
            }
        }

        //不在培训课程的员工
        EmployeeExample employeeExample = new EmployeeExample();
        if(idList.size()>0){
            employeeExample.createCriteria().andEmpIdNotIn(idList);
        }
        List<Employee> notInTrainList = employeeMapper.selectByExample(employeeExample);
        for(Employee list2:notInTrainList){
            EmpVO empVO2 = new EmpVO(list2.getEmpId(), list2.getEmpName(),1);
            empVOList.add(empVO2);
        }
        return empVOList;
    }

    @Override
    public int editTrainEmp(List<Integer> empId, Integer trainId) {
        int result = 0;
        Train train = trainMapper.selectByPrimaryKey(trainId);
        if(train.getTrainCount()>0){
            Train_empExample train_empExample = new Train_empExample();
            train_empExample.createCriteria().andTrainIdEqualTo(trainId);
            result  = train_empMapper.deleteByExample(train_empExample);
        }

            for(Integer idList:empId) {
                Train_emp train_emp = new Train_emp();
                train_emp.setEmpId(idList);
                train_emp.setTrainId(trainId);
                result = train_empMapper.insertSelective(train_emp);
            }

        Train train2 = new Train();
        train2.setTrainCount(empId.size());
        train2.setTrainId(trainId);
        updateTrainByPrimaryKey(train2);
        return result;
    }

    @Override
    public int changeLeaderIdByEmpName(TrainForm trainForm) {

        Integer leaderId = employeeMapper.getEmpIdByLeaderName(trainForm.getEmpName());

        System.out.println("负责人id:"+leaderId);
        System.out.println("课程id:"+trainForm.getTrainId());

        Train train = new Train();
        BeanUtils.copyProperties(trainForm,train);
        train.setLeaderId(leaderId);



        int result = updateTrainByPrimaryKey(train);
        return result;
    }

    public List<TrainVO> toVO(List<Train> trainList){
        List<TrainVO> trainVOList = new ArrayList<TrainVO>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for(Train train:trainList){
            TrainVO vo = new TrainVO();
            BeanUtils.copyProperties(train,vo);
            if(train.getTrainTime()!=null){
                String start = sdf.format(train.getTrainTime());
                vo.setTrainTime(start);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(train.getTrainTime());
                System.out.println("日期："+train.getTrainTime());
                if(train.getTrainDuration()!=null){
                    calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+train.getTrainDuration());
                    vo.setEndTime(sdf.format(calendar.getTime()));
                }
            }
            if(train.getLeaderId()!=null){
                Employee employee = new Employee();
                employee.setEmpId(train.getLeaderId());
                employee = employeeMapper.selectByPrimaryKey(train.getLeaderId());
                vo.setEmpName(employee.getEmpName());
            }
            trainVOList.add(vo);
        }
        return trainVOList;
    }

}